package com.hrzyit.whistle.im.ui.activity

import android.animation.ObjectAnimator
import android.annotation.TargetApi
import android.app.Activity
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.os.Build
import android.os.Bundle
import android.view.ViewTreeObserver
import android.view.Window
import android.view.animation.DecelerateInterpolator
import com.bumptech.glide.Glide
import com.hrzyit.whistle.im.R
import com.hrzyit.whistle.im.entity.FullImageInfo
import kotlinx.android.synthetic.main.activity_full_image.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * 作者：Rance on 2016/12/15 15:56
 * 邮箱：rance935@163.com
 */
class FullImageActivity : Activity() {

    private var mLeft: Int = 0
    private var mTop: Int = 0
    private var mScaleX: Float = 0.toFloat()
    private var mScaleY: Float = 0.toFloat()
    private var mBackground: Drawable? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        setContentView(R.layout.activity_full_image)
//        ButterKnife.bind(this)
        EventBus.getDefault().register(this)

        mFullImageIv.setOnClickListener {
            activityExitAnim(Runnable {
                finish()
                overridePendingTransition(0, 0)
            })
        }
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true) //在ui线程执行
    fun onDataSynEvent(fullImageInfo: FullImageInfo) {
        val left = fullImageInfo.locationX
        val top = fullImageInfo.locationY
        val width = fullImageInfo.width
        val height = fullImageInfo.height
        mBackground = ColorDrawable(Color.BLACK)
        mFullLay!!.background = mBackground
        mFullImageIv!!.viewTreeObserver.addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener {
            override fun onPreDraw(): Boolean {
                mFullImageIv!!.viewTreeObserver.removeOnPreDrawListener(this)
                val location = IntArray(2)
                mFullImageIv!!.getLocationOnScreen(location)
                mLeft = left - location[0]
                mTop = top - location[1]
                mScaleX = width * 1.0f / mFullImageIv!!.width
                mScaleY = height * 1.0f / mFullImageIv!!.height
                activityEnterAnim()
                return true
            }
        })
        Glide.with(this).load(fullImageInfo.imageUrl).into(mFullImageIv)
    }

    private fun activityEnterAnim() {
        mFullImageIv!!.pivotX = 0f
        mFullImageIv!!.pivotY = 0f
        mFullImageIv!!.scaleX = mScaleX
        mFullImageIv!!.scaleY = mScaleY
        mFullImageIv!!.translationX = mLeft.toFloat()
        mFullImageIv!!.translationY = mTop.toFloat()
        mFullImageIv!!.animate().scaleX(1f).scaleY(1f).translationX(0f).translationY(0f).setDuration(500)
            .setInterpolator(DecelerateInterpolator()).start()
        val objectAnimator = ObjectAnimator.ofInt(mBackground, "alpha", 0, 255)
        objectAnimator.interpolator = DecelerateInterpolator()
        objectAnimator.duration = 500
        objectAnimator.start()
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private fun activityExitAnim(runnable: Runnable) {
        mFullImageIv!!.pivotX = 0f
        mFullImageIv!!.pivotY = 0f
        mFullImageIv!!.animate().scaleX(mScaleX).scaleY(mScaleY).translationX(mLeft.toFloat())
            .translationY(mTop.toFloat())
            .withEndAction(runnable).setDuration(500).setInterpolator(DecelerateInterpolator()).start()
        val objectAnimator = ObjectAnimator.ofInt(
            mBackground, "alpha", 255, 0)
        objectAnimator.interpolator = DecelerateInterpolator()
        objectAnimator.duration = 500
        objectAnimator.start()
    }

    override fun onBackPressed() {
        activityExitAnim(Runnable {
            finish()
            overridePendingTransition(0, 0)
        })
    }

    override fun onDestroy() {
        EventBus.getDefault().unregister(this)
        super.onDestroy()
    }

}
